package com.qs.eggs.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Handler;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.drawee.view.SimpleDraweeView;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.qs.eggs.Constants;
import com.qs.eggs.R;
import com.qs.eggs.view.activity.AddAddressNewActivity;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Response;


public class FooAdapter extends RecyclerView.Adapter<FooAdapter.ViewHolder> implements View.OnClickListener{
    private LayoutInflater mInflater;
    private List<Map<String, Object>> footlist=new ArrayList<>();
    private Context context;

    private Handler mHandler;
    private final int REFRESH=5;

    public FooAdapter(Context context, List<Map<String, Object>> footlist, Handler mHandler)
    {
        mInflater = LayoutInflater.from(context);
        this.footlist = footlist;
        this.context=context;
        this.mHandler=mHandler;
    }

    private OnItemClickListener mOnItemClickListener = null;

    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }
    public class ViewHolder extends RecyclerView.ViewHolder {
        public ViewHolder(View arg0) {
            super(arg0);
        }

        private TextView proname,price;
        private ImageView img;
        private SimpleDraweeView pic;

    }

    @Override
    public int getItemCount()
    {
        return footlist.size();
    }

    /**
     * 创建ViewHolder
     */
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType)
    {
        View view = mInflater.inflate(R.layout.item_collect,
                viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(view);
        view.setOnClickListener(this);
        viewHolder.proname = (TextView) view.findViewById(R.id.tv_proname);
        viewHolder.price = (TextView) view.findViewById(R.id.tv_proprice);
        viewHolder.img = (ImageView) view.findViewById(R.id.img_edit);
        viewHolder.pic = (SimpleDraweeView) view.findViewById(R.id.propic);
        return viewHolder;
    }


    /**
     * 设置值
     */
    @Override
    public void onBindViewHolder(ViewHolder viewHolder, final int position) {
        viewHolder.itemView.setTag(position);
      viewHolder.proname.setText(footlist.get(position).get("proname").toString());
      viewHolder.price.setText("¥"+footlist.get(position).get("saleprice").toString());
        Picasso.with(context).load(R.mipmap.icon_del).into(viewHolder.img);
        Picasso.with(context).load(Constants.URL_BASE+"productimages/"+footlist.get(position).get("autoname")).into(viewHolder.pic);
        viewHolder.img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                delete(footlist.get(position).get("collectid").toString());
            }
        });
    }
    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取position
            mOnItemClickListener.onItemClick(v,(int)v.getTag());
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
    //执行删除操作
    private void delete(String s){
        HashMap<String,String> map=new HashMap<String, String>();
        map.put("ids",s);
        Gson gson=new Gson();
        String str=gson.toJson(map);
        OkGo.post(Constants.URL_BASE+"lookhistory?action=deleteLookHistory")
                .params("data",str)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                    System.out.print("删除足迹=="+s);
                        if (s.equals("\"true\"")) {
                            Toast.makeText(context, "删除成功", Toast.LENGTH_SHORT).show();
                            mHandler.sendEmptyMessage(5);
                        }
                    }
                });

    }
}
